// 封装原生axios
import axios from "axios";

const service = axios.create({
    baseURL: "http://localhost:8888",
    timeout: 3000000
});

// 请求拦截器
service.interceptors.request.use(config => {
    // 需要祥config的请求头中添加token
    config.headers['token'] = sessionStorage.getItem("token");
    console.log(config);
    return config;
}, error => {
    console.log(error);
    return Promise.reject(error);
});

// 响应拦截器
service.interceptors.response.use(response => {
    console.log(response);
    if (response && response['data']['code'] == 400) {
        // 过期可以将token删除
        sessionStorage.removeItem("token");
        // 验证不通过的
        window.location.href = '/login';
    }
    return response;
}, error => {
    console.log(error);
    return Promise.reject(error);
});

export default service;